import OverlayBase from './overlayBase';

class CustomLabelOverlay extends OverlayBase {
  constructor(options) {
    super(options);
    let {
      positions,
      labelType = 'default',
      label,
      labelStyle = null,
      payload = null,
    } = options;
    let opts = {
      labelStyle: {
        color: '#fff',
        fontSize: '12px',
        fontWeight: 'bold',
        textShadow: '1px 1px 1px #fff',
        paddingLR: 5,
        ...labelStyle,
        payload,
      },
    };
    // 还需要判断是否是二维数组
    let centerPosition = [];
    if (Array.isArray(positions) && Array.isArray(positions[0])) {
      if (L.LineUtil.isFlat(positions)) {
        centerPosition = positions[0];
      } else {
        centerPosition = positions[0][0];
      }
    } else {
      centerPosition = positions;
    }
    // console.log(centerPosition, positions, L.LineUtil.isFlat(positions));?
    const width =
      label?.length * opts?.labelStyle?.fontSize +
        opts.labelStyle.paddingLR * 2 || 50;
    let myIcon = null;
    if (labelType === 'default') {
      myIcon = L.divIcon({
        className: 'my-div-icon',
        html: `<div style="width:${width}px;color:${opts?.labelStyle?.color};">${label}</div>`,
        iconSize: [width, 20],
        iconAnchor: [width / 2, 20],
      });
    } else {
      myIcon = L.divIcon({
        className: 'my-div-icon',
        html: `<div style="width:${width}px;color:${opts?.labelStyle?.color};">${label}</div>`,
        iconSize: [0, 0],
      });
    }
    return L.marker(centerPosition, { icon: myIcon });
  }
}

export default CustomLabelOverlay;
